package com.eshop.modules.product.service.mapper;

import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.eshop.modules.product.domain.StoreProduct;
import com.eshop.common.mapper.CoreMapper;

@Repository
public interface StoreProductMapper extends CoreMapper<StoreProduct>
{
    @Update({ "update store_product set stock=stock-#{num}, sales=sales+#{num} where id=#{productId} and stock >= #{num}" })
    int decStockIncSales(@Param("num") final Integer num, @Param("productId") final Long productId);
    
    @Update({ "update store_product set stock=stock+#{num}, sales=sales-#{num} where id=#{productId}" })
    int incStockDecSales(@Param("num") final Integer num, @Param("productId") final Long productId);
    
    @Update({ "update store_product set is_show = #{status} where id = #{id}" })
    void updateOnsale(@Param("status") final Integer status, @Param("id") final Long id);
    
    @Update({ "update store_combination set stock=stock-#{num}, sales=sales+#{num} where id=#{activityId} and stock >= #{num}" })
    int decCombinationStockIncSales(@Param("num") final Integer num, @Param("productId") final Long productId, @Param("activityId") final Long activityId);
    
    @Update({ "update store_seckill set stock=stock-#{num}, sales=sales+#{num} where id=#{activityId} and stock >= #{num}" })
    int decSeckillStockIncSales(@Param("num") final Integer num, @Param("productId") final Long productId, @Param("activityId") final Long activityId);
    
    @Update({ "update store_combination set stock=stock+#{num}, sales=sales-#{num} where id=#{activityId} and stock >= #{num}" })
    void incCombinationStockIncSales(@Param("num") final Integer num, @Param("productId") final Long productId, @Param("activityId") final Long activityId);
    
    @Update({ "update store_seckill set stock=stock+#{num}, sales=sales-#{num} where id=#{activityId} and stock >= #{num}" })
    void incSeckillStockIncSales(@Param("num") final Integer num, @Param("productId") final Long productId, @Param("activityId") final Long activityId);
    
    @Update({ "update store_product set browse=browse+1 where id=#{productId}" })
    int incBrowseNum(@Param("productId") final Long productId);
    
    @Delete({ "DELETE from system_attachment where name like CONCAT(#{id},'_%',#{name}, '%')" })
    void deleteForwardImg(@Param("id") final Long id, @Param("name") final String name);
    
    @Select({ "SELECT id,spec_type,ot_price,mer_use,description,is_postage,is_sub,is_best,(sales+ficti) as sales,price,is_bargain,vip_price,store_name,stock,keyword,image,cost,is_good,unit_name,is_benefit,update_time,give_integral,is_new,sort,slider_image,is_show,bar_code,postage,code_path,create_time,cate_id,is_seckill,mer_id,temp_id,ficti,store_info,is_del,is_hot,is_integral,integral,browse FROM store_product ${ew.customSqlSegment}" })
     <E extends IPage<StoreProduct>> E selectPage(final E page, @Param("ew") final Wrapper<StoreProduct> queryWrapper);
}
